package com.lhr.common.web;

import com.lhr.common.domain.Pagination;
import com.lhr.common.util.StringUtil;
import com.lhr.common.util.ValidateUtil;

public class EL {
	private static final int LEFT = 4;
	private static final int RIGHT = 5;
	private static final String JAVASCRIPT = "javascript:";

	public static String pagination(Pagination<?> pagination,
			String href) {
		if (pagination == null)
			return "";
		StringBuffer html = new StringBuffer();
		html.append("<div class='pagination'>");
		html.append("<div class='quotes'>");
		if (pagination.isPreviousPage()) {
			appendTagA(html, converHref(href, 1), "&nbsp;&lt;&lt;&nbsp;", "首页");
			appendTagA(html, converHref(href, pagination.getPageIndex() - 1), "&nbsp;&lt;&nbsp;", "上一页");
		} else {
			html.append("<span class='disabled'>&nbsp;&lt;&lt;&nbsp;</span>");
			html.append("<span class='disabled'>&nbsp;&lt;&nbsp;</span>");
		}

		bulidPageIndexInfo(href, pagination.getPageIndex(), pagination.getPageCount(), html);

		if (pagination.isNextPage()) {
			appendTagA(html, converHref(href, pagination.getPageIndex() + 1), "&nbsp;&gt;&nbsp;", "下一页");
			appendTagA(html, converHref(href, pagination.getPageCount()), "&nbsp;&gt;&gt;&nbsp;", "尾页");
		} else {
			html.append("<span class='disabled'>&nbsp;&gt;&nbsp;</span>");
			html.append("<span class='disabled'>&nbsp;&gt;&gt;&nbsp;</span>");
		}

		html.append("<div style='float: right'>&nbsp;");
		StringUtil.merge(html, "<span class='note'>总页数:", pagination.getPageCount(), "</span>", "&nbsp;");
		StringUtil.merge(html, "<span class='note'>总记录数:", pagination.getTotalCount(), "</span>");
		html.append("</div>");

		html.append("</div>");
		html.append("</div>");

		return html.toString();
	}

	/**
	 * 左右补齐算法 <br/>
	 * 在这初始是左4右5，如果超过4就往右补给
	 */
	private static void bulidPageIndexInfo(String href,
			int pageIndex,
			int pageCount,
			StringBuffer html) {
		int finalLeft = 0;
		int finalRight = 0;

		// 如果当前页大于(LEFT)4的话，向左循环则是4
		if (pageIndex > LEFT) {
			finalLeft += LEFT;
		} else { // 如果是小于(LEFT)4,向左循环则等于当前页减去1，向右循环LEFT(4)-finalLeft
			finalLeft += pageIndex - 1;
			finalRight += LEFT - finalLeft;
		}

		int rightDeviation = (RIGHT + pageIndex) - pageCount;
		// 当前页加上固定的RIGHT(5)是否超过总页数
		if (rightDeviation > 0) {
			finalRight += RIGHT - rightDeviation;
			finalLeft += rightDeviation;
		} else {
			finalRight += RIGHT;
		}

		// 检查finalLeft
		if (finalLeft - pageIndex >= 0) {
			finalLeft = pageIndex - 1;
		}
		// 检查finalRight
		if ((finalRight + pageIndex) > pageCount) {
			finalRight = pageCount - pageIndex;
		}

		for (int i = pageIndex - finalLeft; i < pageIndex; i++) {
			appendTagA(html, converHref(href, i), i, null);
		}

		// 当前页
		StringUtil.merge(html, "<span class='current'>", pageIndex, "</span>");

		for (int i = pageIndex + 1; i <= finalRight + pageIndex; i++) {
			appendTagA(html, converHref(href, i), i, null);
		}

	}

	private static StringBuffer appendTagA(StringBuffer html,
			String href,
			Object value,
			String title) {
		html.append("<a ");
		if (ValidateUtil.isNotEmpty(title)) {
			StringUtil.merge(html, "title='", title, "'");
		}

		if (href.startsWith(JAVASCRIPT)) {
			StringUtil.merge(html, "onclick='", href, "' ");
			StringUtil.merge(html, "href='javascript:void(0);'>", value, "</a>");
		} else {
			StringUtil.merge(html, "href='", href, "'>", value, "</a>");
		}

		return html;
	}

	private static String converHref(String href,
			Object obj) {
		return href.replace("#{pageIndex}", obj.toString());
	}
}
